package com.yztc.love_travel.ui.fourtrip;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.love_travel.R;
import com.yztc.love_travel.bean.fourtrip.FourTripDetailsBean;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;
import com.yztc.love_travel.ui.fourtrip.presenter.FourTripPresenterImpl;
import com.yztc.love_travel.ui.fourtrip.presenter.IFourTripPresenter;
import com.yztc.love_travel.ui.fourtrip.view.IFourTripDetailsView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "四大推荐旅行"的列表的Item的详情页面
 */
public class FourTripDetailsActivity extends AppCompatActivity implements IFourTripDetailsView {

    @BindView(R.id.four_trip_details_toolBar)
    Toolbar toolBar;
    @BindView(R.id.four_trip_header_iv)
    ImageView headerIv; //头图片
    @BindView(R.id.four_trip_title_tv)
    TextView titleTv; //标题
    @BindView(R.id.four_trip_intro_tv)
    TextView introTv; //简介
    @BindView(R.id.four_trip_address_tv)
    TextView addressTv; //景点地址
    @BindView(R.id.four_trip_busLine_iv)
    TextView busLineIv; //公交路线
    @BindView(R.id.four_trip_openTime_iv)
    TextView openTimeIv; //开放时间

    private Context mContext;
    private IFourTripPresenter mPresenter;
    private int destinationId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four_trip_details);
        ButterKnife.bind(this);

        mContext = this;
        mPresenter = new FourTripPresenterImpl(null, this);

        initToolBar();
        getDestinationId();
        loadData();
    }

    //初始化ToolBar
    private void initToolBar() {
        toolBar.setTitle("");
        setSupportActionBar(toolBar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            //设置返回按钮图标
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    //加载菜单布局
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_article_details,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        if(item.getItemId() == android.R.id.home){
            //返回键
            finish();
        }
        return true;
    }

    //加载数据
    private void loadData() {
        mPresenter.getFourTripDetails(URLConstant.FourTripDetails.URL_FOUR_TRIP_DETAILS_BASE, destinationId);
    }

    //接收数据
    private void getDestinationId() {
        Intent intent = getIntent();
        destinationId = intent.getIntExtra(OtherConstant.KEY_DESTINATION_ID, 0);
    }

    @Override
    public void loadStart() {

    }

    @Override
    public void loadFinish() {

    }

    @Override
    public void loadError() {

    }

    //加载数据成功，显示数据到界面
    @Override
    public void showFourTripDetailsData(FourTripDetailsBean fourTripDetailsBean) {
        FourTripDetailsBean.DestinationBean destination = fourTripDetailsBean.getDestination();
        //获取图片的地址
        String picUrl = URLConstant.FourTripDetails.URL_HEADER_PIC + destination.getPicUrl();
        Glide.with(mContext).load(picUrl)
                .placeholder(R.mipmap.logo)
                .error(R.mipmap.logo)
                .into(headerIv); //加载头图片
        titleTv.setText(destination.getTitle()); //设置标题
        introTv.setText(destination.getIntro()); //设置简介
        addressTv.setText("地址："+destination.getAddress()); //设置地址
        busLineIv.setText("路线："+destination.getBusLine()); //设置公交路线
        openTimeIv.setText("开放时间:"+destination.getShopHours()); //设置开放时间
        //设置ToolBar的标题
        if(destination.getTitle().length() > 7){
            toolBar.setTitle(destination.getTitle().substring(0,6)+".......");
        }else {
            toolBar.setTitle(destination.getTitle());
        }
    }
}
